package com.whsxt.config;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import com.alibaba.dubbo.config.annotation.Reference;
import com.whsxt.domain.User;
import com.whsxt.service.IUserService;

public class UserRealm extends AuthorizingRealm{
	
	/*
	 * @Autowired private UserMapper userMapper;
	 */
	@Reference
	private IUserService iUserService;
	
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		 String loginname = (String) token.getPrincipal();
	//	 QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
	//	 queryWrapper.eq("loginname", loginname);
		 User user = iUserService.selectOneByLoginname(loginname);
		//盐是用户名
		if(user != null) {
		ByteSource credentialsSalt = ByteSource.Util.bytes(user.getName());
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), credentialsSalt , this.getName());
			return info;
		}
		return null;
	}
	
}
